package com.zhuhjay.ruiji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhuhjay.ruiji.common.CustomException;
import com.zhuhjay.ruiji.domain.Category;
import com.zhuhjay.ruiji.domain.Dish;
import com.zhuhjay.ruiji.domain.Setmeal;
import com.zhuhjay.ruiji.mapper.CategoryMapper;
import com.zhuhjay.ruiji.mapper.DishMapper;
import com.zhuhjay.ruiji.mapper.SetmealMapper;
import com.zhuhjay.ruiji.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author ZhuHJay
 * @since 2022-05-15
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void checkRemove(Long id) {
        // 查询是否关联了菜单
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        if( dishMapper.selectCount(dishLambdaQueryWrapper) > 0 ){
            // 抛出业务异常
            throw new CustomException("当前分类下关联了菜品, 不能删除");
        }
        // 查询是否关联了套餐
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        if( setmealMapper.selectCount(setmealLambdaQueryWrapper) > 0 ){
            // 抛出业务异常
            throw new CustomException("当前分类下关联了套餐, 不能删除");
        }
        // 删除分类
        removeById(id);
    }
}
